// 百度坐标转高德（传入经度、纬度）
export function bd_decrypt(bd_lng, bd_lat) {
  const X_PI = (Math.PI * 3000.0) / 180.0;
  const x = bd_lng - 0.0065;
  const y = bd_lat - 0.006;
  const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
  const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
  const gg_lng = z * Math.cos(theta);
  const gg_lat = z * Math.sin(theta);
  return { lng: gg_lng, lat: gg_lat };
}

// 高德坐标转百度（传入经度、纬度）
export function bd_encrypt(gg_lng, gg_lat) {
  const X_PI = (Math.PI * 3000.0) / 180.0;
  const x = gg_lng,
    y = gg_lat;
  const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
  const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
  const bd_lng = z * Math.cos(theta) + 0.0065;
  const bd_lat = z * Math.sin(theta) + 0.006;
  return { bd_lat, bd_lng };
}

let count = 0;

// 获取当前坐标
export function getCurrentCoordinate() {
  if (count != 0) return;
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: 'wgs84', // 返回可以用于 uni.openLocation 的经纬度
      success: (res) => {
        resolve({ lng: res.longitude, lat: res.latitude });
      },
      fail: (res) => {
        count++;
        reject(res);
      }
    });
  });
}

// 导航到指定经纬度
export function goDestination(obj) {
  uni.openLocation({
    longitude: obj.lng,
    latitude: obj.lat,
    name: obj.name,
    address: obj.address,
    success: (res) => {
      console.log(res);
    },
    fail: (res) => {
      console.log(res);
    }
  });
}
